package gfo

class MatchshipController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [matchshipInstanceList: Matchship.list(params), matchshipInstanceTotal: Matchship.count()]
    }

    def create = {
        def matchshipInstance = new Matchship()
        matchshipInstance.properties = params
        return [matchshipInstance: matchshipInstance]
    }

    def save = {
        def matchshipInstance = new Matchship(params)
//        def ind = 0
//        print(params.action+", ")
//        print(params.player.id)
//        for (item in params){
//            print(ind+": ")
//            print(item)
//            print(", ")
//            ind++
//        }
//        def match1 = Match.findAllById(1)
//        print match1.players[0]
        if (matchshipInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'matchship.label', default: 'Matchship'), matchshipInstance.id])}"
            redirect(action: "show", id: matchshipInstance.id)
        }
        else {
            render(view: "create", model: [matchshipInstance: matchshipInstance])
        }
    }

    def show = {
        def matchshipInstance = Matchship.get(params.id)
        if (!matchshipInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matchship.label', default: 'Matchship'), params.id])}"
            redirect(action: "list")
        }
        else {
            [matchshipInstance: matchshipInstance]
        }
    }

    def edit = {
        def matchshipInstance = Matchship.get(params.id)
        if (!matchshipInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matchship.label', default: 'Matchship'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [matchshipInstance: matchshipInstance]
        }
    }

    def update = {
        def matchshipInstance = Matchship.get(params.id)
        if (matchshipInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (matchshipInstance.version > version) {
                    
                    matchshipInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'matchship.label', default: 'Matchship')] as Object[], "Another user has updated this Matchship while you were editing")
                    render(view: "edit", model: [matchshipInstance: matchshipInstance])
                    return
                }
            }
            matchshipInstance.properties = params
            if (!matchshipInstance.hasErrors() && matchshipInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'matchship.label', default: 'Matchship'), matchshipInstance.id])}"
                redirect(action: "show", id: matchshipInstance.id)
            }
            else {
                render(view: "edit", model: [matchshipInstance: matchshipInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matchship.label', default: 'Matchship'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def matchshipInstance = Matchship.get(params.id)
        if (matchshipInstance) {
            try {
                matchshipInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'matchship.label', default: 'Matchship'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'matchship.label', default: 'Matchship'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'matchship.label', default: 'Matchship'), params.id])}"
            redirect(action: "list")
        }
    }
}
